/**
 * 
 */
package org.statewalker.parser.turtle.tokenizer;

import org.statewalker.parser.turtle.TurtleDict1;
import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.ITokenizer;
import org.statewalker.tokenizer.StreamToken;
import org.statewalker.tokenizer.CharStream.Marker;
import org.statewalker.tokenizer.base.SpaceTokenizer;

/**
 * @author kotelnikov
 */
public class WSTokenizer implements ITokenizer {

    public static final WSTokenizer INSTANCE = new WSTokenizer();

    private CommentTokenizer fCommentTokenizer;

    private String fKey;

    private SpaceTokenizer fSpacesTokeniser;

    /**
     * 
     */
    public WSTokenizer() {
        this(
            TurtleDict1._SPACES,
            CommentTokenizer.INSTANCE,
            SpaceTokenizer.INSTANCE);
    }

    /**
     * 
     */
    public WSTokenizer(
        String spacesKey,
        CommentTokenizer commentTokenizer,
        SpaceTokenizer spaceTokenizer) {
        fKey = spacesKey;
        fSpacesTokeniser = spaceTokenizer;
        fCommentTokenizer = commentTokenizer;
    }

    public StreamToken read(CharStream stream) {
        WSToken result = null;
        Marker marker = stream.markPosition();
        try {
            while (true) {
                StreamToken token = fCommentTokenizer.read(stream);
                if (token == null)
                    token = fSpacesTokeniser.read(stream);
                if (token == null)
                    break;
                if (result == null)
                    result = new WSToken(fKey);
                result.appendToken(token);
            }
            if (result != null) {
                result.init(stream, marker);
            }
            return result;
        } finally {
            marker.close(result == null);
        }
    }
}
